package com.aicre.wuliuapp.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aicre.wuliuapp.app.MainActivity;
import com.aicre.wuliuapp.app.R;
import com.aicre.wuliuapp.app.view.MarqueeTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class DetailActivity extends BaseActivity {
    private SharedPreferences sp;
    private TextView start_detail;
    private TextView finish_detail;
    private TextView name_detail;
    private TextView phone_detail;
    private MarqueeTextView kind_cargo_detail;

    private TextView length_detail;
    private TextView weight_detail;

    private TextView type_car_detail;
    private TextView price_detail;
    private MarqueeTextView deadline_detail;
    private TextView cubage;
    private TextView comment;
    private Button btn_call;

    private HashMap<String,String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ActionBar bar = getSupportActionBar();
        bar.setTitle("详细信息");
        bar.setDisplayHomeAsUpEnabled(true);
        start_detail = (TextView) findViewById(R.id.start_detail);
        finish_detail = (TextView) findViewById(R.id.finish_detail);
        name_detail = (TextView) findViewById(R.id.contact);
        phone_detail = (TextView) findViewById(R.id.phone);
        kind_cargo_detail = (MarqueeTextView) findViewById(R.id.kind);

        length_detail = (TextView) findViewById(R.id.length);
        weight_detail = (TextView) findViewById(R.id.weight);

        type_car_detail = (TextView) findViewById(R.id.car);
        price_detail = (TextView) findViewById(R.id.price);
        deadline_detail = (MarqueeTextView) findViewById(R.id.date);
        cubage=(TextView)findViewById(R.id.cubage);
        comment=(TextView)findViewById(R.id.comment);
        Intent i = getIntent();
        map = (HashMap)i.getSerializableExtra("id");

        btn_call=(Button)findViewById(R.id.dialBtn);





        start_detail.setText(map.get("fp")+" "+map.get("fc")+" "+map.get("fd")+"     至");
        finish_detail.setText(map.get("tp")+" "+map.get("tc")+" "+map.get("td"));

        name_detail.setText(map.get("name"));

        sp=getSharedPreferences("56huowu", Activity.MODE_WORLD_READABLE);
        String id=sp.getString("id", "-1").trim();
        if(!map.get("phone").isEmpty()){

            if(map.get("phone").length()!=11){
                phone_detail.setText(map.get("phone"));
                btn_call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DetailActivity.this,"无效的电话号码",Toast.LENGTH_SHORT).show();
                    }
                });

            }else{

                if(!id.equals("-1")){
                    btn_call.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+map.get("phone")));
                            startActivity(intent);
                        }
                    });
                    phone_detail.setText(map.get("phone"));
                }else {
                    StringBuffer sb = new StringBuffer(map.get("phone"));
                    sb.replace(3, 7, "****");
                    btn_call.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(DetailActivity.this,"您还没有登录",Toast.LENGTH_SHORT).show();

                        }
                    });
                    phone_detail.setText(sb.toString());
                }

            }

        }





        if(!map.get("gn").equals("")) {
            kind_cargo_detail.setText(map.get("gn"));
        }

        if(!map.get("cs").equals("")) {
            StringBuffer sb=new StringBuffer(map.get("cs"));
            sb.append("米");
            length_detail.setText(sb.toString());
        }

        if(!map.get("gw").equals("")) {
            StringBuffer sb=new StringBuffer(map.get("gw"));
            sb.append("吨");
            weight_detail.setText(sb.toString());
        }

        if(!map.get("ct").equals("")) {
            type_car_detail.setText(map.get("ct"));
        }
        if(!map.get("price").equals("")) {
            StringBuffer sb=new StringBuffer(map.get("price"));
            sb.append("元");
            price_detail.setText(sb.toString());
        }
//        Log.i("Price", map.get("price"));
        if(!map.get("date").equals("")) {
            DateFormat format1=new SimpleDateFormat("MM/dd/yyyy");
            DateFormat format2=new SimpleDateFormat("yyyy年MM月dd日");
            Date date=null;
            String str=map.get("date");
            try {
                date=format1.parse(str);
                deadline_detail.setText(format2.format(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }


        }
        if(!map.get("gv").equals("")){
            StringBuffer sb=new StringBuffer(map.get("gv"));
            sb.append("方");
            cubage.setText(sb.toString());
        }
        if(!map.get("info").equals("")){
            comment.setText(map.get("info"));
        }

    }



    public void Jsontomap(String Json) {

        JSONObject obj;
        try {
            obj = new JSONObject(Json);
            map.put("fp", obj.getString("fp"));
            map.put("fc", obj.getString("fc"));
            map.put("fd", obj.getString("fd"));
            map.put("tp", obj.getString("tp"));
            map.put("tc", obj.getString("tc"));
            map.put("td", obj.getString("td"));
            map.put("gn", obj.getString("gn"));
            //map.put("gt", obj.getString("gt"));
            map.put("ct", obj.getString("ct"));
            map.put("gm", obj.getString("gm"));
            map.put("trant", obj.getString("trant"));
            map.put("price", obj.getString("price"));
            map.put("cs", obj.getString("cs"));
            map.put("name", obj.getString("name"));
            map.put("phone", obj.getString("phone"));
            map.put("date", obj.getString("date"));
            map.put("deadline", obj.getString("deadline"));
            map.put("einfo", obj.getString("einfo"));
        } catch (JSONException e)
        {
            e.toString();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);

    }

}
